/*
 * 文件名: NavigateTabActivity.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2012-3-1
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package com.longevityassistant.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import app.log.APPLog;
import app.utils.Tools;
import app.widget.PAPopupWindow;
import app.widget.ScrollerNavigateView;
import app.widget.ScrollerNavigateView.OnMenuItemSelectListener;

/**
 *  导航公共类
 * @author EX-XIAOFANQING001
 * @date 2012-3-1
 * @version [Android PABank C01, @2012-3-1]
 * @description
 */
public abstract class NavigateTabActivity extends MenuActivity
{
    
    public static final class Param {
        public static final String SELECT_INDEX = "select_index";
    }
    
    public static final String TAG = NavigateTabActivity.class.getSimpleName();
    
    /**
     * 本地Activity管理器
     */
    private LocalActivityManager mLocalActivityManager;
    
    /**
     * 支持滑动的Tab选择器
     */
    private ScrollerNavigateView mScrollerMenu;
    
    /**
     * 当前的Tab列表
     */
    private List<TabInfo> mTabInfos;
    
    /**
     * 上次选择的Tab的位置
     */
    private int mLastPosition;
    
    /**
     * 根View  
     */
    private LinearLayout llRoot;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigate_main_tab);

        mLocalActivityManager = getLocalActivityManager();

        mScrollerMenu = (ScrollerNavigateView) findViewById(R.id.scroll_menu);
        llRoot = (LinearLayout) findViewById(R.id.ll_root);
        
        List<TabInfo> tabs = initTabs();
        setTabs(tabs);
        
        // 默认显示第一个导航
        if (tabs != null && tabs.size() > 0) {
            
            Intent i = getIntent();
            
            int selectIndex = i.getIntExtra(Param.SELECT_INDEX, 0);
            
            setCurrentTab(selectIndex, tabs.get(selectIndex).getParams());
        }
    }
    
    /**
     * 设置Tab 信息
     * @param tabs
     */
    private  void setTabs(final List<TabInfo> tabs)
    {
        if (tabs == null) {
            return ;
        }
        
        mTabInfos = tabs;
        
        List<ScrollerNavigateView.Menu> menus = new ArrayList<ScrollerNavigateView.Menu>();
        for (int i=0; i<tabs.size(); i++) {
            TabInfo ti = tabs.get(i);
            
            ScrollerNavigateView.Menu menu = new ScrollerNavigateView.Menu();
            menu.title = ti.getTitle();;
            menu.icon = ti.getIconResId();
            
            menus.add(menu);
        }
        
        mScrollerMenu.setMenus(menus);
        
        mScrollerMenu.setOnMenuItemSelectListener(new OnMenuItemSelectListener()
        {
            @Override
            public void OnMenuItemSelect(int menuPosition)
            {
                PAPopupWindow.getInstance().dismissPopupWindow();
                
                if (!onScollerMenuSelect(menuPosition, mLastPosition))  {
                    TabInfo tabInfo = mTabInfos.get(menuPosition);
                    Map<String, String> params = tabInfo.getParams();
                    setCurrentTab(menuPosition, params);
                    
                    onTabSelected(menuPosition);
                }
            }
        });
    }
    
    protected abstract List<TabInfo> initTabs();
    
    protected void onTabSelected(int tabId) 
    {
        mLastPosition = tabId;
    }
    
    /**
     * 选择指定的Tab
     * @param tabId
     * @param params
     */
    public void setCurrentTab(int tabId, Map<String, String> params) {
        TabInfo tabInfo = mTabInfos.get(tabId);
        
        Intent i  = tabInfo.getContent();
        
        if(params != null && params.size() > 0) {
            for (String key : params.keySet()) {
                String value = params.get(key);
                i.putExtra(key, value);
            }
        }
        
        setCurrentTab(tabId, i);
    }
    
    private int currentTabId = -1;
    
    /**
     * 显示指定的子Activity
     * @param tabId
     * @param intent
     */
    public void setCurrentTab(int tabId, Intent intent){
        mScrollerMenu.select(tabId, true);
        
        // 离开上一个TAB的回调
        try {
            NavigateTabChild leaveTabChild = (NavigateTabChild) mLocalActivityManager.getActivity(String.valueOf(currentTabId));
            if (leaveTabChild != null) {
                APPLog.i(TAG, "child:" + currentTabId + " leave...........");
                leaveTabChild.onLeaveTabChild();
            }
        } catch (Exception e) {
            // ignore
        }
        
        Window window = mLocalActivityManager.startActivity(String.valueOf(tabId), intent);
        View view = window.getDecorView();
        
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
        lp.setMargins(0, 0, 0, -Tools.getActualSize(this, 9));
        
        if (llRoot.getChildCount() > 1) {
            llRoot.removeViewAt(0);
        }
        
        view.setLayoutParams(lp);
        
        llRoot.addView(view, 0);
        
        // 重新显示TAB的回调
        try {
            NavigateTabChild intoTabChild = (NavigateTabChild) mLocalActivityManager.getActivity(String.valueOf(tabId));
            if (intoTabChild != null) {
            	APPLog.i(TAG, "child:" + tabId + " resume.........");
                intoTabChild.onIntoTabChild();
            }
        } catch (Exception e) {
            // ignore
        }
        
        currentTabId = tabId;
    }
    
    public boolean onScollerMenuSelect(int position, int lastPosition) {
        return false;
    }
    
    /**
     * 要添加的Tab信息
     * @author EX-XIAOFANQING001
     * @version [Android PABank C01, 2012-3-1]
     */
    public static class TabInfo {
        private String title;
        private int iconResId;
        private Intent content;
        
        private Map<String, String> params;
            
        public TabInfo() {
            
        }

        public TabInfo(String title, int iconResId, Intent content)
        {
            super();
            this.title = title;
            this.iconResId = iconResId;
            this.content = content;
        }

        public String getTitle()
        {
            return title;
        }

        public void setTitle(String title)
        {
            this.title = title;
        }

        public int getIconResId()
        {
            return iconResId;
        }

        public void setIconResId(int iconResId)
        {
            this.iconResId = iconResId;
        }

        public Intent getContent()
        {
            return content;
        }

        public void setContent(Intent content)
        {
            this.content = content;
        }

        public void setParams(Map<String, String> params)
        {
            this.params = params;
        }

        public Map<String, String> getParams()
        {
            return params;
        }
    }
    
    public interface NavigateTabChild {
        
        public void onIntoTabChild();
        
        public void onLeaveTabChild();
    }
}
